#include <cstdio>
#include <queue>

const int N = 405, M = N * N;
int n, m, s, t, w[M >> 1], e_cnt, heads[N], d[N];
std::queue<int> q;

struct Edge {
  int v, nxt;
} e[M];

inline void add(int u, int v) {
  e[++e_cnt].v = v, e[e_cnt].nxt = heads[u], heads[u] = e_cnt;
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen64("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen64("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("graph.in", "r", stdin);
  freopen("graph.out", "w", stdout);
#endif
#endif

  scanf("%d%d", &n, &m);
  scanf("%d%d", &s, &t);
  for (int i = 1, u, v; i <= m; ++i) {
    scanf("%d%d", &u, &v);
    add(u, v), add(v, u);
  }
  q.push(s);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    if (u == t) goto end;
    for (int i = heads[u], v; i; i = e[i].nxt) {
      if (d[v = e[i].v] > d[u]) w[(i + 1) >> 1] = d[u] + 1;
      if (!d[v = e[i].v] && v != s) {
        d[v] = w[(i + 1) >> 1] = d[u] + 1;
        q.push(v);
      }
    }
  }
end:
  printf("%d\n", d[t]);
  for (int i = 1; i <= m; ++i) { printf("%d\n", std::min(d[t], w[i])); }
  return 0;
}